package org.reliablesource.prism.gef.editor.model;

import org.eclipse.draw2d.Bendpoint;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;

public class WireBendpoint implements java.io.Serializable, Bendpoint, IPropertySource {

	private static final long serialVersionUID = 1045757625068591208L;

	private float weight = 0.5f;

	private Dimension d1, d2;
	
	private java.awt.Point location;
	private Wire parent;

	private final static String LOCATION = "WireBendPt.Location"; 
	private final static String SEQ_NUM = "WireBendPt.SeqNum";
	
	private IPropertyDescriptor [] descriptors;
	public WireBendpoint() {
		descriptors  = new IPropertyDescriptor[] 
		            			{new PropertyDescriptor(LOCATION, "Location:"),
		            				new PropertyDescriptor(SEQ_NUM, "Seq. Num:")
		            				};
		((PropertyDescriptor)descriptors[0]).setLabelProvider(new ILabelProvider(){

			public Image getImage(Object element) {
				return null;
			}

			public String getText(Object element) {
				Point loc = (Point) element;
				StringBuffer buffer = new StringBuffer();
				buffer.append("("+loc.x +"," + loc.y +" )");
				return buffer.toString().trim();
			}

			public void addListener(ILabelProviderListener listener) {
			}

			public void dispose() {
			}

			public boolean isLabelProperty(Object element, String property) {
				return false;
			}

			public void removeListener(ILabelProviderListener listener) {
			}
			
		});
	}

	public Dimension getFirstRelativeDimension() {
		return d1;
	}

	/**
	 * Return the Location of this PointerLocation.
	 * 
	 * @return a non-null location instance
	 */
	public Point getLocation() {
		return new Point(location.x, location.y);
	}
	
	public java.awt.Point getJLocation(){
		return location;
	}
	
	public void setLocation(final Point loc) {
		setPropertyValue(LOCATION, loc);
	}

	public Dimension getSecondRelativeDimension() {
		return d2;
	}

	public float getWeight() {
		return weight;
	}

	public void setRelativeDimensions(final Dimension dim1, final Dimension dim2) {
		d1 = dim1;
		d2 = dim2;
	}

	public void setWeight(final float w) {
		weight = w;
	}

	public Object getEditableValue() {
		return null;
	}

	public IPropertyDescriptor[] getPropertyDescriptors() {
		return 	descriptors;
	}

	public Object getPropertyValue(Object id) {
		if(LOCATION.equals(id)){
			return location;
		}else if(SEQ_NUM.equals(id))
			return new Integer(getSequenceNum());
		return null;
	}

	public boolean isPropertySet(Object id) {
		return false;
	}

	public void resetPropertyValue(Object id) {
		
	}

	public void setPropertyValue(Object id, Object value) {
		if (LOCATION.equals(id)) {
			if(location == null)
				location = new java.awt.Point();
			Object old = new Point(location.x, location.y);
			
			Point newPt = (Point)value;
			location.setLocation(newPt.x, newPt.y);
		} 
	}
	
	public void setParent(final Wire _parent){
		parent = _parent;
	}
	
	public Wire getParent(){
		return parent;
	}
	
	public int getSequenceNum(){
		return parent.getBendpoints().indexOf(this);
	}
}	
